package com.worldcup.controller.template;

import com.worldcup.service.ITravelSearchLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author duwan.zq
 * @date 2018/12/05
 */
@Controller
@RequestMapping("/admin/statistics")
public class StatisticsController {

    @Autowired
    private ITravelSearchLogService travelSearchLogService;

    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView view = new ModelAndView("/statistics/travel_statistics.html");
        return view;
    }

    @RequestMapping("/count/list")
    @ResponseBody
    public List listCount(){
        Map<String,Integer> countMap = travelSearchLogService.statisticsSearchCount();
        List<Map<String, Object>> result = new ArrayList(countMap.size());
        countMap.forEach((name, count) -> {
            Map<String, Object> map = new HashMap<>(4);
            map.put("name", name);
            map.put("count", count);
            result.add(map);
        });
        return result.stream().sorted(Comparator.comparing(map -> ((Integer)map.get("count")))).collect(Collectors.toList());
    }

}
